import './public-path'
import Vue from 'vue'
import store from './store'
import router, { resetRouter } from './router'
import MainApp from './views/main/mainApp'
import './plugins/element'
import './plugins/avue'
import './assets/iconfont/iconfont.css'
import './router/permission'
import './directives/hasPermission'

Vue.config.productionTip = false

let instance = null
let isFetchApp = true

export function setRoutes (routes, fetchApp) {
  isFetchApp = typeof (fetchApp) === 'undefined' ? true : fetchApp
  resetRouter(routes)
  if (!window.__POWERED_BY_QIANKUN__) {
    render()
  }
}

function init (props) {
  instance = new Vue({
    router,
    store,
    render: (h) => h(MainApp)
  }).$mount('#app')
}

function render (props) {
  beforeLoad().then(r => {
    init(props)
  }).catch(e => {
    init(props)
  })
}

export async function bootstrap () {

}

/**
 * 应用每次进入都会调用 mount 方法，通常我们在这里触发应用的渲染方法
 */
export async function mount (props) {
  render(props)
}

/**
 * 应用每次 切出/卸载 会调用的方法，通常在这里我们会卸载微应用的应用实例
 */
export async function unmount () {
  instance.$destroy()
  instance = null
}

async function beforeLoad () {
  if (isFetchApp) {
    await store.dispatch('getOem')

    console.group('系统初始化')
    console.log('系统编号', window.appCode)

    await store.dispatch('getApp', { appCode: window.appCode })

    console.log('系统信息加载完成！')
    console.log('系统编号：', store.state.base.app.appCode, '系统名称：', store.state.base.app.appName)
    console.groupEnd()
  }
}
